// Copyright (c) 2022 Erwin Kok. BSD-3-Clause license. See LICENSE file for more details.
package com.gitee.wsl.transform.encode.base

import com.gitee.wsl.ext.tryOrNull
import com.gitee.wsl.transform.encode.ascii.encodeToAsciiString
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals


class Base58Test {

    @Test
    fun random() {
        val random = Random
        for (j in 1 until 256) {
            val b = ByteArray(j)
            //for (i in 0 until 100) {
                println()
                random.nextBytes(b)
                println("b:" + b.contentToString()+"->${b.decodeToString()}")
                val inBtc = b.encodeBase58()
                println("inBtc:$inBtc")
                val outBtc = tryOrNull{ inBtc.decodeBase58() }?:continue
                println("out:"+outBtc.contentToString())
                assertContentEquals(b, outBtc)
            //}
        }
    }

    @Test
    fun randomString() {
        for (j in 1 until 256) {
            val b = ByteArray(j)
            //for (i in 0 until 100) {
            println()
            val str = Random.nextBytes(b).encodeToAsciiString
            println("b:" + str)
            val inBtc = str.encodeBase58()
            println("inBtc:${inBtc.contentToString()}")
            val outBtc = inBtc.decodeBase58()
            println("out:"+ outBtc)
            assertEquals(str, outBtc)
            //}
        }
    }

    @Test
    fun empty() {
        listOf(
            "1QCaxc8hutpdZ62iKZsn1TCG3nh7uPZojq",
            "1DhRmSGnhPjUaVPAj48zgPV9e2oRhAQFUb",
            "17LN2oPYRYsXS9TdYdXCCDvF2FegshLDU2",
            "14h2bDLZSuvRFhUL45VjPHJcW667mmRAAn",
        ).forEach { a->
            val outBtc = a.decodeBase58()
            val b = outBtc.encodeBase58()
            assertEquals(a,b)
        }
    }

//    @Test
//    fun empty() {
//        assertErrorResult("can not decode zero-length string") { Base58.decodeStringBtc("") }
//    }
//
//    @Test
//    fun `invalid character`() {
//        assertErrorResult("invalid base58 digit (*)") { Base58.decodeStringBtc("*&%^") }
//    }
//
//    @Test
//    fun `high bit`() {
//        assertErrorResult("high-bit set on invalid digit") { Base58.decodeStringBtc("😂") }
//    }
}
